package com.wyait.manage.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.inject.internal.cglib.proxy.$InvocationHandler;
import com.wyait.manage.dao.CarouselMapper;
import com.wyait.manage.pojo.Carousel;
import com.wyait.manage.utils.ExceptionEnum;
import com.wyait.manage.utils.PageDataResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

@Service
public class CarouselServiceImpl implements CarouselService{
    @Value("${zm.upload.imagePath}")
    private String imagePath; //图片存储路径
    @Value("${zm.upload.imageUrl}")
    private String imageUrl;   //图片访问路径
    @Autowired(required = false)
    private CarouselMapper carouselMapper;

    /**
     * 分页查询轮播图
     */
    @Override
    public PageDataResult getCaroutleList(Carousel carousel, Integer page, Integer limit) {
        PageDataResult pdr = new PageDataResult();
        PageHelper.startPage(page, limit);
        List<Carousel> carousels = carouselMapper.getcarouselList(carousel);
        // 获取分页查询后的数据
        PageInfo<Carousel> pageInfo = new PageInfo<>(carousels);
        // 设置获取到的总记录数total：
        pdr.setTotals(Long.valueOf(pageInfo.getTotal()).intValue());
        pdr.setList(carousels);
        return pdr;
    }


    /**
     * 图片上传  返回图片名称字符串
     */
    @Override
    public Map<String, String> save(MultipartFile file) {
        // 需求：把文件保存到nginx服务器
        //生成随机文件名称
        String uuid = UUID.randomUUID().toString();
        //获取文件原名
        String originalFilename = file.getOriginalFilename();
        //获取后缀名
        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
        //文件名称(新的图片名称)
        String fileName = uuid +extName;


        try {
            file.transferTo(new File(imagePath,fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取图片完整访问路径
        String imgUrl = imageUrl + fileName;
        //返回图片完整访问路径和图片名
        HashMap<String, String> map = new HashMap<>();
        map.put("src",imgUrl);
        map.put("title",fileName);
        return map;
    }


    /**
     * 获取轮播图列表
     * @return
     */
    @Override
    public List<Carousel> getCarousels() {
        List<Carousel> carouselList = carouselMapper.getCarousels();
        return carouselList;
    }

    /**
     * 轮播图[新增或更新]
     */
    @Override
    public String setCarousel(Carousel carousel) {
        int i = 0;
        //如果id不为空执行更新操作
        if(carousel.getId() != null){
            carousel.setCreateTime(new Date());
            i = carouselMapper.updateCarousel(carousel);
        }else{
            //否则执行新增操作
            carousel.setCreateTime(new Date());
            i = carouselMapper.insert(carousel);
        }
        if (i != 0){
            return "ok";
        }else{
            return "[新增或更新]轮播图失败";
        }
    }


    /**
     *根据id删除轮播图
     */
    @Override
    public String delCarousel(Integer id) {
        //删除本地图片
        if(id != null){
            //根据id获取 轮播图url(http://172.25.35.62/index-images/3aab310d-7add-4895-bdc3-616ebeff2ffa.jpg)
            String imgUrl = carouselMapper.getImgUrlById(id);
            //截取图片名字(3aab310d-7add-4895-bdc3-616ebeff2ffa.jpg)
            File file = new File(imgUrl);
            String name = file.getName();

            //拼接成本地访问路径地址
            String img_address = imagePath + "\\" +name;
            //删除图片
            File del_img = new File(img_address);
            del_img.delete();
        }
        //删除数据库数据
        int i = carouselMapper.delCarousel(id);
        if (i != 0){
            return "ok";
        }else{
            return "删除失败";
        }

    }




}
